<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>人脸识别验证</title>
    <script src="js/faceUtil.js"></script>
    <script src="js/jquery.js"></script>
    <script src="js/vue.min.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #myApp {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            text-align: center;
            max-width: 550px;
            width: 90%;
            backdrop-filter: blur(10px);
        }
        
        h1 {
            color: #333;
            margin-bottom: 30px;
            font-weight: 600;
            font-size: 28px;
            letter-spacing: 1px;
        }
        
        #video {
            border: 4px solid #e0e0e0;
            border-radius: 15px;
            overflow: hidden;
            margin: 0 auto 25px;
            width: 450px;
            height: 400px;
            background-color: #f8f9fa;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        button {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 16px 40px;
            font-size: 18px;
            border-radius: 50px;
            cursor: pointer;
            margin-top: 20px;
            transition: all 0.3s ease;
            font-weight: 600;
            letter-spacing: 1px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }
        
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }
        
        button:active {
            transform: translateY(1px);
        }
        
        button:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
        }
        
        .loading {
            display: none;
            margin-top: 20px;
            color: #666;
            font-size: 16px;
        }
        
        .spinner {
            border: 4px solid #f1f2f6;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 24px;
            height: 24px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-right: 12px;
            vertical-align: middle;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        /* 弹窗样式优化 */
        #customModal {
            display: none; 
            position: fixed; 
            z-index: 1000; 
            left: 0; 
            top: 0; 
            width: 100%; 
            height: 100%; 
            background-color: rgba(0,0,0,0.5);
        }
        
        #customModal > div {
            background: linear-gradient(135deg, #ffffff 0%, #f5f7fa 100%);
            margin: 15% auto; 
            padding: 30px; 
            border-radius: 20px; 
            width: 350px; 
            text-align: center; 
            box-shadow: 0 15px 30px rgba(0,0,0,0.2);
            animation: modalOpen 0.3s ease-out;
        }
        
        @keyframes modalOpen {
            from {opacity: 0; transform: translateY(-20px);}
            to {opacity: 1; transform: translateY(0);}
        }
        
        #modalMessage {
            margin: 25px 0; 
            font-size: 18px; 
            color: #333;
            font-weight: 500;
        }
        
        #modalButtons button {
            border: none;
            padding: 12px 25px;
            margin: 0 8px;
            border-radius: 30px;
            color: white;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 3px 10px rgba(0,0,0,0.1);
        }
        
        #modalButtons button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        #confirmBtn {
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
        }
        
        #retryBtn {
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
        }
        
        #redirectBtn {
            background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
        }
    </style>
</head>
<body>
<div id="myApp">
    <h1>人脸识别验证</h1>
    <div id="video"></div>
    <button type="submit" @click="faceCollect" id="verifyBtn">开始验证</button>
    <div class="loading" id="loading">
        <div class="spinner"></div>
        <span>验证中...</span>
    </div>
</div>

<!-- 自定义弹窗 -->
<div id="customModal">
    <div>
        <p id="modalMessage"></p>
        <div id="modalButtons">
            <button id="confirmBtn">确定</button>
            <button id="retryBtn">重试</button>
            <button id="redirectBtn">前往认证</button>
        </div>
    </div>
</div>
</body>
<script>
    new Vue({
        el: '#myApp',
        methods:{
            faceCollect(){
                //console.log("进入faceCollect函数")
                const code = faceUtile.getDecode();
                //faceUtile.collectFace(this.name, this.age, this.phone)
                const params = {
                    image: code
                }
                
                // 显示加载状态
                document.getElementById('verifyBtn').disabled = true;
                document.getElementById('loading').style.display = 'block';
                
                //发送ajax请求
                $.ajax({
                    url: '/face_detect/',
                    type: 'post',
                    data: JSON.stringify(params),
                    success: (res) => {
                        // 隐藏加载状态
                        document.getElementById('verifyBtn').disabled = false;
                        document.getElementById('loading').style.display = 'none';
                        
                        const modal = document.getElementById('customModal');
                        const message = document.getElementById('modalMessage');
                        const confirmBtn = document.getElementById('confirmBtn');
                        const retryBtn = document.getElementById('retryBtn');
                        const redirectBtn = document.getElementById('redirectBtn');
                        
                        if (res.code == 200) {
                            message.textContent = res.msg;
                            retryBtn.style.display = 'none';
                            redirectBtn.style.display = 'none';
                            confirmBtn.style.display = 'inline-block';
                            
                            // 点击确定按钮的处理
                            confirmBtn.onclick = function() {
                                modal.style.display = 'none';
                                console.log("跳转页面");
                                window.location.href = './predict.html';
                            };
                        } else {
                            message.textContent = res.msg;
                            retryBtn.style.display = 'inline-block';
                            redirectBtn.style.display = 'inline-block';
                            confirmBtn.style.display = 'none';
                            
                            // 重试按钮处理
                            retryBtn.onclick = function() {
                                modal.style.display = 'none';
                            };
                            
                            // 认证按钮处理（保持原跳转逻辑）
                            redirectBtn.onclick = function() {
                                modal.style.display = 'none';
                                console.log("跳转页面");
                                window.location.href = './face_collect.html';
                            };
                        }
                        
                        // 显示弹窗
                        modal.style.display = 'block';
                    },
                    error: () => {
                        // 隐藏加载状态
                        document.getElementById('verifyBtn').disabled = false;
                        document.getElementById('loading').style.display = 'none';
                        
                        // 显示错误信息
                        const modal = document.getElementById('customModal');
                        const message = document.getElementById('modalMessage');
                        const confirmBtn = document.getElementById('confirmBtn');
                        const retryBtn = document.getElementById('retryBtn');
                        const redirectBtn = document.getElementById('redirectBtn');
                        
                        message.textContent = '网络请求失败，请重试';
                        retryBtn.style.display = 'inline-block';
                        redirectBtn.style.display = 'inline-block';
                        confirmBtn.style.display = 'none';
                        
                        retryBtn.onclick = function() {
                            modal.style.display = 'none';
                        };
                        
                        redirectBtn.onclick = function() {
                            modal.style.display = 'none';
                            window.location.href = './face_collect.html';
                        };
                        
                        modal.style.display = 'block';
                    }
                })
            }
        }

    })



    $(function () {
        //指定摄像头打开位置
        faceUtile.openVideo("video")
        faceUtile.width=450
        faceUtile.height=400  /* 调整摄像头高度以匹配显示区域 */
    })
</script>
</html>